;; MIT License

;; Copyright (c) 2025 三体自动化技术协会

;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:

;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.

;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.



(let ((* (char->integer #\*))
      (╔ (char->integer #\╔))
      (╗ (char->integer #\╗))
      (╝ (char->integer #\╝))
      (╚ (char->integer #\╚))
      (═ (char->integer #\═))
      (║ (char->integer #\║)))
    `(
      ;; set x in addr 0
      p 0 p 32 store
      ;; set y in addr 1
      p 1 p 32 store
      ;; set direction 0=right 1=up 2=left 3=down in addr 2
      p 2 p 0 store
      ;; set '*' in addr 3
      p 3 p ,* store
      ;; set targe curve level
      ;; 设置曲线的阶数
      p 4 p 2  store
      ;; main
      call B
      halt
      A: p 4 load p 0 eq jumpif END-A
         p 4 load dec p 4 swap store  ; 深度-1
         call T- call B call F call T+ call A
         call F
         call A  call T+ call F call B call T-
         p 4 load inc p 4 swap store  ; 深度恢复
      END-A:  ret
      B: p 4 load p 0 eq jumpif END-B
         p 4 load dec p 4 swap store  ; 深度-1
         call T+
         call A call F
         call T-
         call B
         call F
         call B  call T- call F call A call T+
         p 4 load inc p 4 swap store  ; 深度恢复
      END-B: ret
      F:     p 2 load p 0 neq jumpif 1? call east  ret
      1?:    p 2 load p 1 neq jumpif 2? call north ret
      2?:    p 2 load p 2 neq jumpif 3? call west  ret
      3?:                               call south ret
          east:  call x++ call DRAW═  ret
	      north: call y++ call DRAW║  ret
	      west:  call x-- call DRAW═  ret
	      south: call y-- call DRAW║  ret
      T+:
            call debug
            p 2 load p 0 eq callif DRAW╝ ; 0->1
            p 2 load p 1 eq callif DRAW╗ ; 1->2
            p 2 load p 2 eq callif DRAW╔ ; 2->3
            p 2 load p 3 eq callif DRAW╚ ; 3->0
            call d++
            call debug
            ret
      T-:
            call debug
            p 2 load p 0 eq callif DRAW╗ ; 0->3
            p 2 load p 3 eq callif DRAW╝ ; 3->2
            p 2 load p 2 eq callif DRAW╚ ; 2->1
            p 2 load p 1 eq callif DRAW╔ ; 1->0
            call d--
            call debug
            ret
      DRAW*: p 0 load p 1 load p ,* setu ret
      DRAW╔: p 0 load p 1 load p ,╔ setu ret
	  DRAW╗: p 0 load p 1 load p ,╗ setu ret
	  DRAW╝: p 0 load p 1 load p ,╝ setu ret
	  DRAW╚: p 0 load p 1 load p ,╚ setu ret
	  DRAW═: p 0 load p 1 load p ,═ setu ret
	  DRAW║: p 0 load p 1 load p ,║ setu ret
      x++:   p 0 load inc p 0 swap store ret
      x--:   p 0 load dec p 0 swap store ret
      y++:   p 1 load dec p 1 swap store ret
      y--:   p 1 load inc p 1 swap store ret
      d++:   p 2 load inc p 4 mod p 2 swap store ret
      d--:   p 2 load dec p 4 mod p 2 swap store ret
      debug:
             p 0 load print
             p 1 load print
             p 2 load print
             p 3 load print
             p 4 load print
      		 ret
      ))
